<?php
//
// THE DAILY PEN FUNCTIONS - RSS BETA VERSION
// By Christian Touzel
//

// GET RSS OBJECT
function GetRSSObject($feedsrc)
{
	$feedobj = new SimplePie();
	$feedobj->set_feed_url($feedsrc);
	$feedobj->handle_content_type();
	try
	{
		$feedobj->init();
	}
	catch (Exception $e) 
	{
		return NULL;
	}
	return $feedobj;
}

// READ FROM RSS
function ReadFromRSS($feedsrc, $headerd, $max, $content, $dayinterval)
{
	$now = new DateTime();
	$retval = "";
	$feedobj = GetRSSObject($feedsrc);
	if (is_null($feedobj) || is_null($feedobj->get_item(0)))
	{
		return "";
	}
	$feedstamp = new DateTime($feedobj->get_item(0)->get_date());
	$feedstamp->setTimezone(new DateTimeZone('America/Montreal'));
	$since_start = floor(($now->format('U') - $feedstamp->format('U')) / (60*60*24));
	if ($dayinterval=="-1" || $since_start<=$dayinterval)
	{
		$retval = $retval."<h3>".$headerd."</h3>";
		for ($i=0; $i<$max; $i++) 
		{
			$retval = $retval . "<p><b>". $feedobj->get_item($i)->get_title(). "</b><br/>";
			$enclosure = $feedobj->get_item($i)->get_enclosure();
			if ($content)
			{
				$retval = $retval . $feedobj->get_item($i)->get_content()."</p>";
			}
			else
			{
				$retval = $retval . $feedobj->get_item($i)->get_description()."</p>";
			}
		}
	}
	return $retval;
}

// GET HEADLINES FROM RSS
function GetHeadlinesFromRSS($feedsrc, $max, $dayinterval)
{
    $retval = "";
    try
    {
        $now = new DateTime();

        $feedobj = GetRSSObject($feedsrc);
        $feedstamp = new DateTime($feedobj->get_item(0)->get_date());
        $feedstamp->setTimezone(new DateTimeZone('America/Montreal'));
        $since_start = floor(($now->format('U') - $feedstamp->format('U')) / (60*60*24));
        if ($dayinterval=="-1" || $since_start<=$dayinterval)
        {
            for ($i=0; $i<$max; $i++)
            {
				if (is_null($feedobj->get_item($i)))
				{
					return "";
				}
                $retval = $retval . "<p>". $feedobj->get_item($i)->get_title(). "</p>";
            }
        }
    }
    catch (Exception $e)
    {
        $lognow = new DateTime();
        $lognow->setTimezone(new DateTimeZone('America/Montreal'));
        $GLOBALS['errors'] = $GLOBALS['errors'] . "<p>".$lognow->format('H:i:s')."
            - ERROR - Unable to get headlines (".$e->getMessage().")</p>";
    }
	return $retval;
}

// READ FROM COMICS RSS 
function ReadFromComicsRSS($feedsrc, $headerd, $max, $content, $dayinterval)
{
	$now = new DateTime();
	$retval = "";
	$feedobj = GetRSSObject($feedsrc);
	if (is_null($feedobj) || is_null($feedobj->get_item(0)))
	{
		return "";
	}
	$feedstamp = new DateTime($feedobj->get_item(0)->get_date());
	$feedstamp->setTimezone(new DateTimeZone('America/Montreal'));
	$since_start = floor(($now->format('U') - $feedstamp->format('U')) / (60*60*24));
	if ($dayinterval=="-1" || $since_start<=$dayinterval)
	{
		$retval = $retval."<h3>".$headerd."</h3>";
		for ($i=0; $i<$max; $i++) 
		{
			$retval = $retval . "<p>";
			if ($content)
			{
				$htmlval = $feedobj->get_item($i)->get_content();
				preg_match('/<img[^>]+>/i',$htmlval, $image);
				$retval = $retval . $image[0]. "</p>";
			}
			else
			{
				$htmlval = $feedobj->get_item($i)->get_description();
				preg_match('/<img[^>]+>/i', $htmlval, $image);
				$retval = $retval . $image[0] . "</p>";
			}
		}
	}
	return $retval;
}

// READ FROM COMICS RSS
// SELECT THE RIGHT IMAGE
function ReadFromComicsRSS_IMG($feedsrc, $headerd, $max, $content, $dayinterval, $imgnum)
{
	$now = new DateTime();
	$retval = "";
	$feedobj = GetRSSObject($feedsrc);
	if (is_null($feedobj) || is_null($feedobj->get_item(0)))
	{
		return "";
	}
	$feedstamp = new DateTime($feedobj->get_item(0)->get_date());
	$feedstamp->setTimezone(new DateTimeZone('America/Montreal'));
	$since_start = floor(($now->format('U') - $feedstamp->format('U')) / (60*60*24));
	if ($dayinterval=="-1" || $since_start<=$dayinterval)
	{
		$retval = $retval."<h3>".$headerd."</h3>";
		for ($i=0; $i<$max; $i++) 
		{
			$retval = $retval . "<p>";
			if ($content)
			{
				$htmlval = $feedobj->get_item($i)->get_content();
				preg_match_all('/<img[^>]+>/i',$htmlval, $image);
				$retval = $retval . $image[0][1]. "</p>";
			}
			else
			{
				$htmlval = $feedobj->get_item($i)->get_description();
				preg_match_all('/<img[^>]+>/i', $htmlval, $image);
				$retval = $retval . $image[0][1] . "</p>";
			}
		}
	}
	return $retval;
}

// READ FROM RSS NO IMAGE
function ReadFromRSS_NoImage($feedsrc, $headerd, $max, $content, $dayinterval)
{
	$output = ReadFromRSS($feedsrc, $headerd, $max, $content, $dayinterval);
    $output = preg_replace("/<img[^>]+\>/i", "", $output); 
	return $output;
}
// READ FROM CYBERPRESSE
function ReadFromCyberpresse($feedsrc, $headerd, $max, $content, $dayinterval)
{
	$now = new DateTime();
	$retval = "";
	try
	{
		$feedobj = GetRSSObject($feedsrc);
		if (is_null($feedobj) || is_null($feedobj->get_item(0)))
		{
			return "";
		}
		$feedstamp = new DateTime($feedobj->get_item(0)->get_date());
		$feedstamp->setTimezone(new DateTimeZone('America/Montreal'));
		$since_start = floor(($now->format('U') - $feedstamp->format('U')) / (60*60*24));
		if ($dayinterval=="-1" || $since_start<=$dayinterval)
		{
			$retval = $retval."<h3>".$headerd."</h3>";
			for ($i=0; $i<$max; $i++) 
			{
				$retval = $retval . "<p><b>". $feedobj->get_item($i)->get_title(). "</b><br>";
				$desc = $feedobj->get_item($i)->get_content();
				$pos = stripos($desc,"<br clear");
				$desc = substr($desc,0,$pos);
				$retval = $retval. $desc . "</p>";
			}
		}
		$retval = preg_replace("/<img[^>]+\>/i", "", $retval); 
	}
	catch (Exception $e) 
	{
		$lognow = new DateTime();
		$lognow->setTimezone(new DateTimeZone('America/Montreal'));
		$GLOBALS['errors'] = $GLOBALS['errors'] . "<p>".$lognow->format('H:i:s')." - ERROR - Unable to retrieve feed from cyberpresse - ".$feedsrc." (".$e->getMessage().")</p>";
	}
	return $retval;
}

// READ OPINIONS FROM CYBERPRESSE
function ReadOpinionsFromCyberpresse($feedsrc, $max)
{
	$retval = "";
	$feedobj = GetRSSObject($feedsrc);
	if (is_null($feedobj) || is_null($feedobj->get_item(0)))
	{
		return "";
	}
	for ($i=0; $i<$max; $i++) 
	{
		try
		{
			$retval = $retval."<h3>LA PRESSE - ".$feedobj->get_title();
			$retval = $retval . $feedobj->get_item($i)->get_title(). "</h3>";
			$desc = GetOpinionContentFromCyberpresse($feedobj->get_item($i)->get_link());
			$retval = $retval. $desc . "</p>";
		}	
		catch (Exception $e) 
		{
			$lognow = new DateTime();
			$lognow->setTimezone(new DateTimeZone('America/Montreal'));
			$GLOBALS['errors'] = $GLOBALS['errors'] . "<p>".$lognow->format('H:i:s')." - ERROR - Unable to parse Cybepresse opinions content (".$feedobj->get_item($i)->get_link().")</p>";
		}
	}
	$retval = preg_replace("/<img[^>]+\>/i", "", $retval); 
	return $retval;
}

// GET OPINION CONTENT FROM CYBERPRESSE
function GetOpinionContentFromCyberpresse($link)
{
	$retval = "";
	try
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $link);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
		$output = curl_exec($ch);
		$doc = new DOMDocument();
		libxml_use_internal_errors(true);
		if ($output=="")
		{
			$lognow = new DateTime();
			$lognow->setTimezone(new DateTimeZone('America/Montreal'));
			$GLOBALS['errors'] = $GLOBALS['errors'] . "<p>".$lognow->format('H:i:s')." - ERROR - Cyberpresse opinion link returned an empty string (".$link.")</p>";
			return "";
		}
		$doc->loadHTML($output);
		$content = $doc->getElementById('maincontent');
		if (is_null($content))
		{
			$lognow = new DateTime();
			$lognow->setTimezone(new DateTimeZone('America/Montreal'));
			$GLOBALS['errors'] = $GLOBALS['errors'] . "<p>".$lognow->format('H:i:s')." - ERROR - Cyberpresse opinion link does not have a 'maincontent' div (".$link.")</p>";
			return "";
		}
		$divs = $content->getElementsByTagName('div');
		foreach($divs as $node) 
		{
			$hoy = $node->getAttribute("class");
			if (stripos($hoy,"excerpt")!==false)
			{
				$retval = $retval. $node->C14N();
			}
			if (stripos($hoy,"chapitreTexte")!==false)
			{
				$retval = $retval. $node->C14N();
			}
		}
	}
	catch (Exception $e) 
	{
		$lognow = new DateTime();
		$lognow->setTimezone(new DateTimeZone('America/Montreal'));
		$GLOBALS['errors'] = $GLOBALS['errors'] . "<p>".$lognow->format('H:i:s')." - ERROR - Unable to retrieve opinions content from cyberpresse - ".$link." (".$e->getMessage().")</p>";
	}
	return $retval;
}

?>